package com.qqtech.qquan.community.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.community.enums.CommunityNoticeStatusDictEnum;
import com.qqtech.qquan.community.model.CommunityBaseQuery;
import com.qqtech.qquan.community.model.CommunityBaseVo;
import com.qqtech.qquan.community.model.CommunityNotice;
import com.qqtech.qquan.community.model.CommunityNoticeQuery;
import com.qqtech.qquan.community.model.CommunityNoticeVo;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.community.service.CommunityNoticeService;
import com.qqtech.qquan.sys.controller.BaseAdminController;
import com.qqtech.qquan.user.model.UserAdmin;
import com.qqtech.qquan.user.service.UserBaseService;

@Controller
@RequestMapping(value = "/community/notice")
public class CommunityNoticeController extends BaseAdminController {
	@Resource
	private CommunityNoticeService communityNoticeService;

	@Resource
	private CommunityBaseService communityBaseService;

	@Resource
	private UserBaseService userBaseService;

	@Override
	protected BaseService<CommunityNotice> getBaseService() {
		return communityNoticeService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/community/communityNotice-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(CommunityNoticeQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "id"));
		if (StringUtil.isNotBlank(query.getSearch())) {
			CommunityBaseQuery communityBaseQuery = new CommunityBaseQuery();
			communityBaseQuery.setSearch(query.getSearch());
			List<CommunityBaseVo> list = communityBaseService.queryList(communityBaseQuery);
			if (null != list && list.size() > 0) {
				List<Integer> includeCommunityIds = new ArrayList<Integer>();
				for (CommunityBaseVo communityBaseVo : list) {
					includeCommunityIds.add(communityBaseVo.getId());
				}
				query.setIncludeCommunityIds(includeCommunityIds);
			}
		}
		Page<CommunityNoticeVo> page = communityNoticeService.queryPageList(query, pageKit);
		CommunityBaseVo communityBaseVo = null;
		for (CommunityNoticeVo communityNoticeVo : page) {
			communityBaseVo = communityBaseService.getById(communityNoticeVo.getCommunityId());
			communityNoticeVo.setCommunityName(communityBaseVo.getName());
		}
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}

	@RequestMapping(value = "/detail")
	public String detail(Integer id, Model model) {
		CommunityNoticeVo detailVo = communityNoticeService.getById(id);
		CommunityBaseVo communityBaseVo = communityBaseService.getById(detailVo.getCommunityId());
		detailVo.setCommunityName(communityBaseVo.getName());
		model.addAttribute("detailVo", detailVo);
		return "/community/communityNotice-detail";
	}

	/**
	 * 创建成功
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/create")
	@ResponseBody
	public Result create(CommunityNotice communityNotice) {
		Result ret = new Result();
		UserAdmin userAdmin = this.getLoginUser();
		communityNotice.setImg("");
		communityNotice.setCreatorId(userAdmin.getId());
		communityNotice.setIsShow(YesNoEnum.YES.getCode());
		communityNotice.setStatus(CommunityNoticeStatusDictEnum.NORMAL.getCode());
		communityNotice.setPv(0);
		communityNoticeService.insert(communityNotice);
		return ret;
	}

	/**
	 * 去新增
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/toAdd")
	public String toAdd(Model model) {
		CommunityBaseQuery communityBaseQuery = new CommunityBaseQuery();
		communityBaseQuery.setIsShow(YesNoEnum.YES.getCode());
		List<CommunityBaseVo> list = communityBaseService.queryList(communityBaseQuery);
		model.addAttribute("list", list);
		return "/community/communityNotice-add";
	}
	/**
	 * 去更新
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toUpdate")
	public String toUpdate(Integer id, Model model) {
		CommunityNoticeVo updateVo = communityNoticeService.getById(id);
		model.addAttribute("updateVo", updateVo);
		CommunityBaseQuery communityBaseQuery = new CommunityBaseQuery();
		communityBaseQuery.setIsShow(YesNoEnum.YES.getCode());
		List<CommunityBaseVo> list = communityBaseService.queryList(communityBaseQuery);
		model.addAttribute("list", list);
		return "/community/communityNotice-update";
	}
	/**
	 * 创建成功
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Result update(CommunityNotice communityNotice) {
		Result ret = new Result();
		communityNoticeService.updateById(communityNotice);
		return ret;
	}
	
	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Result delete(Integer id) {
		Result ret = new Result();
		communityNoticeService.deleteById(id);
		return ret;
	}
}
